
package BOH.GUI;
import BOH.data.EateryDBConnection;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;

/**
 * The Back of House System for a
 * small restaurant.
 * 
 * Maeghan Colvin & Jennifer Haveman
 * CSC 289 0B01
 * February 13, 2014
 */

public class TillCountPanel extends JPanel
{
    // Variables
    private JFrame frame;
    private JPanel mainPanel;               // Holds the mainPanel
    private JPanel optionsPanel;            // Holds the optionsPanel
    private JPanel buttonPanel;             // Holds the buttonPanel
    private JPanel tempPanel;               // Holds tempPanel for formatting
    private JLabel amountLabel;             // Holds the amountLabel
    private JTextField amountTextField;     // Holds the amountTextField
    private SimpleDateFormat dateFormat;    // Holds the date format
    private String sqlStatement;            // Selects the SUM of sales for the day
    private double total;                   // Holds the total sales
    private double cashFlow;                // Holds the gain or loss in till
    private JButton calculateButton;
    
    /**
     * Constructor
     */
    
    public TillCountPanel()
    {
        // Set Look and Feel.
        JFrame.setDefaultLookAndFeelDecorated(true);
        
        // Create and set up the window.
        frame = new JFrame("Till Count");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        // Build and add mainPanel
        this.buildMainPanel();
        frame.add(mainPanel);
        
        // Add content to the window.
        frame.setSize(170, 150);
        frame.validate();
        
        // Display the window.
        frame.setVisible(true);  
    }
    
    /**
     * The buildMainPanel builds the 
     * mainPanel.
     */
    
    private void buildMainPanel() 
    {
        // Create mainPanel
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Get optionsPanel
        buildOptionsPanel();
        
        // Get buttonPanel
        buildButtonPanel();
        
        // Add panels
        mainPanel.add(optionsPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    }
    
     /**
     * The buildOptionsPanel builds the 
     * optionsPanel.
     */
    
    private void buildOptionsPanel() 
    {
        // Create optionsPanel
        optionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        optionsPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create optionsPanel
        tempPanel = new JPanel(new GridLayout(8, 0));
        tempPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create the JLabels and TextFields
        amountLabel = new JLabel("Enter Total Amount: ");
        amountTextField = new JTextField(10);
        amountTextField.setHorizontalAlignment(JTextField.CENTER);
        amountTextField.setToolTipText("Enter amount of till after draw is calculated to 100.");
        
        // Add components to panel
        tempPanel.add(amountLabel);
        tempPanel.add(amountTextField);
        optionsPanel.add(tempPanel);
    }
    
    /**
     * The buildButtonPanel builds the 
     * buttonPanel.
     */
    
    private void buildButtonPanel() 
    {
        // Create buttonPanel
        buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create button
        calculateButton = new JButton("Calculate");
        calculateButton.setMnemonic(KeyEvent.VK_C);
        calculateButton.addActionListener(new CalculateButtonActionListener());
        
        // Add buttonPanel
        buttonPanel.add(calculateButton, BorderLayout.CENTER);
    }
    
    /**
     * The getTillTotal method gets the total
     * money that should be in the till.
     */
    
    public void getTillTotal()
    {
        // Variables
        double[] salesTotals;
        
        // Create current date object
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        
        sqlStatement = "SELECT SUM(Total) " +
                       "FROM Sales " +
                       "WHERE PayDate = '" + dateFormat.format(date) + "'";
      
        // Get the sales amount for the day.
        String sales = EateryDBConnection.getDataString(sqlStatement);
        total = Double.parseDouble(sales);
    }
    
    /**
     * The getTillAmount method gets the total
     * money that is in the till.
     */
    
    public void getTillAmount()
    {
        // Variables
        String till;
        
        // Get the till amount
        till = amountTextField.getText();
        double tillAmount = Double.parseDouble(till);
       
        // Get the amount over or under total sales
        cashFlow = total - tillAmount;
        if (cashFlow > 0)
        {
            System.out.println("Till Amount: " + total + "\n" +
                               "Amount Entered: " + tillAmount + "\n " +
                               "Short By: " + cashFlow);
        }
        else if(cashFlow < 0)
        {
            double cashFlowABS = Math.abs(cashFlow);
            System.out.println("Till Amount: " + total + "\n " +
                               "Amount Entered: " + tillAmount + "\n " +
                               "Over By: " + cashFlowABS);
        }
    }
    
    /*
     * The CalculateButtonActionListener is used to
     * calculate the till information.
     */
    
    private class CalculateButtonActionListener implements ActionListener 
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // Get the till information
            getTillTotal();
            getTillAmount();
        }
    }
}
